//
//  LXRepairApplyViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/24.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXRepairApplyViewController.h"
#import "LXTableViewCell.h"
#import "LXChooseCarViewViewController.h"
#import "LXApplyAnnexTableCell.h"
#import "LXChooseBtnTableViewCell.h"
#import "LXRepairModel.h"
#import "LXRemarkTabelViewCell.h"
#import "LXPhotoHelper.h"
#import "LXChooseManagerViewController.h"
#import "FuZeRenModel.h"
#import "KeHuInfoModel.h"
#import "LXChooseCustomViewController.h"
#import "LXCooperationListViewController.h"
#import "LXCooperationModel.h"
@interface LXRepairApplyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSMutableDictionary *loanDic;
@property (nonatomic) NSDictionary *selDic;
@property (nonatomic) CheLiangModel *carModel; // 车辆数据
@property (nonatomic) NSMutableArray *annexAry; //  维修图片  展示用
@property (nonatomic) NSMutableArray *annexDataAry; //  维修图片 提交用
@property (nonatomic) NSArray *repairStatusData; // 维修状态数据源
@property (nonatomic) NSInteger repairStatus; // 维修状态 1维修中 2已完成
@property (nonatomic) KeHuInfoModel *keHuModel;// 客户模型
@property (nonatomic) NSArray *settlementStatusData; // 结算状态数据源
@property (nonatomic) NSInteger settlementStatus; // 结算状态 1未结算 2已结算 3已确认

@property (nonatomic) NSArray *pickCarStatusData; // 提车状态数据源
@property (nonatomic) NSInteger pickCarStatus; // 提车状态 1未提车 2已提车


@property (nonatomic) NSInteger remindDay; // 到期提醒
@property (nonatomic) NSArray *remindDayData; // 到期提醒数据源



@property (nonatomic) NSString *chargeId; // 负责人id
@property (nonatomic) FuZeRenModel *fuZeModel; //  负责人模型


@property (nonatomic) LXCooperationModel *cooModel; //  合作模型



@end

@implementation LXRepairApplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    if (self.type==1) {
        [self layoutCustomNav:self.view title:@"新增维修"];
    }
    else {
        [self layoutCustomNav:self.view title:@"修改维修记录"];
    }
    
    NSDictionary *dic = @{
        @"url":@"upLoad_B",
    };
    self.annexAry = [NSMutableArray array];
    self.annexDataAry = [NSMutableArray array];
    [self.annexAry addObject:dic];

    
    self.keHuModel = [KeHuInfoModel new];
    
    [self.tableView registerClass:[LXTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LXTableViewCell class])];
    [self.tableView registerClass:[LXApplyAnnexTableCell class]
         forCellReuseIdentifier:NSStringFromClass([LXApplyAnnexTableCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXRemarkTabelViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];

    self.view.backgroundColor = White_Color;
    [self.view addSubview:self.tableView];
    
    [self loadData];
    
    if (self.type==1) {
        [self setBottomViewWithText:@"确认新增"];
    }
    else {
        [self setBottomViewWithText:@"确认修改"];
    }
    
    
    
}
- (void)loadData{
    
    self.loanDic  = [NSMutableDictionary dictionary];
    
    // 默认维修中
    self.repairStatus = 1;
    // 默认未结算
    self.settlementStatus = 1;
    // 默认未提车
    self.pickCarStatus = 1;
    // 默认3天
    self.remindDay = 3;
    
    // 模型为空,说明从车辆详情进入
    if (!ObjectIsEmpty(self.repairModel)) {

        // 车牌号
        [self.loanDic setValue:self.repairModel.carNum forKey:@"carNum"];

        // 给车辆模型赋值,提交时传入
        self.carModel = [CheLiangModel new];
        self.carModel.ID = self.repairModel.carId;
        self.carModel.carNum = self.repairModel.carNum;


        // 修改的时候需要带入数据
        if (self.type==2) {

            // 维修状态
            self.repairStatus = self.repairModel.repairStatus;
            // 结算状态
//            self.settlementStatus = self.repairModel.settlementStatus;
            //提车状态
            self.pickCarStatus = self.repairModel.pickCarStatus;
            //负责人
            self.chargeId = self.repairModel.chargeId;
            [self.loanDic setValue:[NSString stringWithNull:self.repairModel.chargeName] forKey:@"chargeName"];
            self.fuZeModel = [FuZeRenModel new];
            self.fuZeModel.ID = self.chargeId;
            self.fuZeModel.realName = self.repairModel.chargeName;
            
            // 客户姓名
            self.keHuModel = [KeHuInfoModel new];
            self.keHuModel.driverId = self.repairModel.driverId;
            self.keHuModel.customerName = [NSString stringWithNull:self.repairModel.driverName];
            [self.loanDic setValue:[NSString stringWithNull:self.repairModel.driverName] forKey:@"customerName"];
            // 维修厂
            self.cooModel = [LXCooperationModel new];
            self.cooModel.ID = self.repairModel.repairCompanyId;
            self.cooModel.name = self.repairModel.repairCompanyName;
            [self.loanDic setValue:[NSString stringWithNull:self.repairModel.repairCompanyName] forKey:@"repairCompanyName"];
            //维修金额
            [self.loanDic setValue:[NSString stringWithNull:self.repairModel.repairAmount] forKey:@"repairAmount"];
            //进厂日期
            [self.loanDic setValue:[NSString stringWithNull:self.repairModel.intoTime] forKey:@"intoTime"];
            //应付日期
            [self.loanDic setValue:[NSString stringWithNull:self.repairModel.intoTime] forKey:@"exceptPayDate"];
            //出厂日期
            [self.loanDic setValue:[NSString stringWithNull:self.repairModel.outTime] forKey:@"outTime"];
            //公里数
            [self.loanDic setValue:[NSString stringWithNull:self.repairModel.distance] forKey:@"distance"];
            //维修内容
            [self.loanDic setValue:StringIsEmpty(self.repairModel.repairContent)?@"无":self.repairModel.repairContent forKey:@"repairContent"];
            //备注
            [self.loanDic setValue:self.repairModel.remark forKey:@"remark"];
            //维修图片
            NSArray *urls = [self.repairModel.repairPic componentsSeparatedByString:@","];
            for (NSString *url in urls) {
                if(StringIsEmpty(url))continue;
                NSString *imageUrl = [NSString stringWithFormat:@"%@%@",HTTP_URL,url];
                [self.annexAry insertObject:@{@"url":imageUrl} atIndex:0];
                [self.annexDataAry insertObject:url atIndex:0];
            }
            
        }



    }

    NSArray *ary0 = @[
         
           @{
               @"title":@"*车牌号",
               @"key":@"carNum",
               @"placeHolder" : @"请选择车牌号",
               @"canEdit":@(NO),
               @"showAccess":ObjectIsEmpty(self.repairModel)?@(YES):@(NO),
               @"maxLength":@(1000),
               @"sel":ObjectIsEmpty(self.repairModel)?@"clickCarNum":@"",
               @"height":@(kComHeight)
           },
           @{
               @"title":@"负责人",
               @"key":@"chargeName",
               @"placeHolder" : @"请选择负责人",
               @"canEdit":@(NO),
               @"showAccess":self.type==1?@(YES):@(NO),
               @"maxLength":@(1000),
               @"sel":self.type==1?@"clickManager":@"",
               @"height":@(kComHeight)
           },
           @{
               @"title":@"租车客户",
               @"key":@"customerName",
               @"placeHolder" : @"请选择租车客户",
               @"canEdit":@(NO),
               @"showAccess":self.type==1?@(YES):@(NO),
               @"maxLength":@(1000),
               @"sel":self.type==1?@"clickCustomerName":@"",
               @"height":@(kComHeight)
           },
           @{
               @"title":self.type==1?@"*进厂日期":@"进厂日期",
               @"key":@"intoTime",
               @"placeHolder" : @"请选择进厂日期",
               @"canEdit":@(NO),
               @"showAccess":self.type==1?@(YES):@(NO),
               @"maxLength":@(1000),
               @"sel":self.type==1?@"clickIntoTime":@"",
               @"height":@(kComHeight)
           },
           @{
               @"title":self.type==1?@"*应付维修金额(元)":@"应付维修金额(元)",
               @"key":@"repairAmount",
               @"placeHolder" : @"请输入维修金额",
               @"keyboardType":@(UIKeyboardTypeDecimalPad),
               @"canEdit":self.type==1?@(YES):@(NO),
               @"showAccess":@(NO),
               @"maxLength":@(10),
               @"sel":@"",
               @"height":@(kComHeight)
           },
           @{
               @"title":self.type==1?@"*应付日期":@"应付日期",
               @"key":@"exceptPayDate",
               @"placeHolder" : @"请选择应付日期",
               @"canEdit":@(NO),
               @"showAccess":self.type==1?@(YES):@(NO),
               @"maxLength":@(1000),
               @"sel":self.type==1?@"clickExceptPayTimeAction":@"",
               @"height":@(kComHeight)
           },
           @{
               @"title":self.type==1?@"*修理厂":@"修理厂",
               @"key":@"repairCompanyName",
               @"placeHolder" : @"请选择修理厂",
               @"canEdit":@(NO),
               @"showAccess":self.type==1?@(YES):@(NO),
               @"maxLength":@(10),
               @"sel":self.type==1?@"clickRepairDepotAction":@"",
               @"height":@(kComHeight)
           },

           @{
               @"title":@"维修状态",
               @"key":@"repairStatus",
               @"height":@([LXChooseBtnTableViewCell height:self.repairStatusData])
           },
           @{
               @"title":@"出厂日期",
               @"key":@"outTime",
               @"placeHolder" : @"请选择出厂日期",
               @"canEdit":@(NO),
               @"showAccess":self.type==1?@(YES):@(NO),
               @"maxLength":@(1000),
               @"sel":self.type==1?@"clickOutTime":@"",
               @"height":@(kComHeight)
           },
           @{
               @"title":@"当前公里数(Km)",
               @"key":@"distance",
               @"placeHolder" : @"请输入当前公里数",
               @"canEdit":self.type==1?@(YES):@(NO),
               @"showAccess":@(NO),
               @"inputType":@(NUM_DECIMAL),
               @"keyboardType":@(UIKeyboardTypeDecimalPad),
               @"maxLength":@(7),
               @"sel":@"",
               @"height":@(kComHeight)
           },
           
           @{
               @"title":@"维修附加图片(最多9张)",
               @"key":@"annex",
               @"height":self.type==1?@(kComAnnexHeight):@([LXApplyAnnexTableCell getCellHeightWithDataSource:self.annexAry])
           }
    ];
    [self.data addObject:ary0];
    
    
    NSArray *ary1 = @[
    
//        @{
//            @"title":@"结算状态",
//            @"key":@"settlementStatus",
//            @"height":@([LXChooseBtnTableViewCell height:self.settlementStatusData])
//        },
        @{
            @"title":@"提车状态",
            @"key":@"pickCarStatus",
            @"height":@([LXChooseBtnTableViewCell height:self.settlementStatusData])
        },
        
//        @{
//            @"title":@"到期提醒",
//            @"key":@"remindDay",
//            @"height":self.type==1?@([LXChooseBtnTableViewCell height:self.remindDayData]):@(0.0001)
//        },

        @{
            @"title":@"维修内容",
            @"key":@"repairContent",
            @"placeHolder" : @"请输入维修内容...",
            @"canEdit":self.type==1?@(YES):@(NO),
            @"height":@([NSString remarksHeight:self.loanDic[@"repairContent"]])
        },
        @{
            @"title":@"备注",
            @"key":@"remark",
            @"placeHolder" : @"请输入备注...",
            @"canEdit":@(YES),
            @"height":@(150)
        },
    ];
    
    [self.data addObject:ary1];
  
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.data.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.data[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.data[indexPath.section][indexPath.row];
    if ([dic[@"key"] isEqualToString:@"annex"]) {
        return [LXApplyAnnexTableCell getCellHeightWithDataSource:self.annexAry] + 30;
    }
    return [dic[@"height"] doubleValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *dic = self.data[indexPath.section][indexPath.row];

    // 维修图片
    if ([dic[@"key"] isEqualToString:@"annex"]) {
        LXApplyAnnexTableCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXApplyAnnexTableCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
        cell.titleLab.text = dic[@"title"];
        [cell reloadAnnexData:self.annexAry WithMaxNum:9];
        
        
        
        // 添加图片
        [cell setCallBackTakeAnnex:^(NSInteger num) {
            
            [[LXPhotoHelper shareHelper] showImageViewSelcteWithResultBlock:^(id data) {

                [self uploadImage:data];

            } andMultipleImageBlock:^(id data) {

                [self uploadImage:data];

            } andSelectedNum:9 - self.annexDataAry.count];
            
        }];
        
        
        // 放大图片
        [cell setCallBackExtendAnnex:^(NSInteger num,id obj) {
            LXBigImageView *view = [[LXBigImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) WithShowImageAry:self.annexDataAry];
            [view.scrollView setContentOffset:CGPointMake(ScreenWidth*num, 0) animated:NO];
            [[UIApplication sharedApplication].delegate.window addSubview:view];
            
        }];
        // 删除图片
        [cell setCallBackDeleteAnnex:^(NSInteger num) {
            
            [self.annexAry removeObjectAtIndex:num];
            [self.annexDataAry removeObjectAtIndex:num];

            [self.tableView reloadData];
        }];
        
        
        
        return cell;
    }
    // 维修状态 | 结算状态 | 提车状态 | 到期提醒 | 应付订单
   else if ([dic[@"key"] isEqualToString:@"repairStatus"]||
//            [dic[@"key"] isEqualToString:@"settlementStatus"]||
            [dic[@"key"] isEqualToString:@"pickCarStatus"]||
            [dic[@"key"] isEqualToString:@"remindDay"]) {
       
       NSArray *tempAry;
       
       if ([dic[@"key"] isEqualToString:@"repairStatus"]) {
           tempAry = self.repairStatusData;
       }
       else if([dic[@"key"] isEqualToString:@"settlementStatus"]){
           tempAry = self.settlementStatusData;
       }
       else if([dic[@"key"] isEqualToString:@"pickCarStatus"]){
           tempAry = self.pickCarStatusData;
       }
       else if([dic[@"key"] isEqualToString:@"remindDay"]){
           tempAry = self.remindDayData;
       }
       
       
       LXChooseBtnTableViewCell *cell = [[LXChooseBtnTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([LXChooseBtnTableViewCell class]) WithData:tempAry];

       cell.selectionStyle = UITableViewCellSelectionStyleNone;
       [cell setClipsToBounds:YES];
     
       cell.titleLab.text = dic[@"title"];
       [cell.titleLab addAttributes:TextAttrCorlor(ColorWithEA545A) forString:@"*"];

       cell.canEdit = YES;
       [cell setClickBtnBlock:^(NSInteger Id) {
           if ([dic[@"key"] isEqualToString:@"repairStatus"]) {
               self.repairStatus = Id;
           }
           else if([dic[@"key"] isEqualToString:@"settlementStatus"]){
               self.settlementStatus = Id;
           }
           else if([dic[@"key"] isEqualToString:@"pickCarStatus"]){
               self.pickCarStatus = Id;
           }
           else if([dic[@"key"] isEqualToString:@"remindDay"]){
               self.remindDay = Id;
           }
       }];
       
       
       
       return cell;
    }
    // 维修内容 备注
   else if([dic[@"key"]  isEqualToString:@"repairContent"] ||
           [dic[@"key"] isEqualToString:@"remark"]){
       LXRemarkTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
       [cell setClipsToBounds:YES];
       cell.textView.placeholder = dic[@"placeHolder"];
       cell.textView.maxLength = 200;
       cell.remarkTitle.text = dic[@"title"];
       cell.textView.text = self.loanDic[dic[@"key"]];
       cell.textView.userInteractionEnabled = [dic[@"canEdit"] boolValue];
       
       
       if ([dic[@"canEdit"] boolValue]) {
           cell.textView.maxLength = 200;
       }
       else {
           cell.textView.wordNumLabel.text = @"";
       }

       
       [cell setTextViewEdit:^(NSString * _Nonnull textStr) {
           self.loanDic[dic[@"key"]]  = textStr;
       }];
       return cell;
   }
    
    LXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setClipsToBounds:YES];
    

    cell.cellDic = dic;
    cell.textFiled.hidden = NO;
    cell.subTitleLab.hidden = YES;
    cell.textFiled.text = self.loanDic[dic[@"key"]];
    

    cell.textfiledChangeBlock = ^(NSString * _Nonnull textStr) {
        self.loanDic[dic[@"key"]]  = textStr;
    };
    
    
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    view.backgroundColor = [UIColor colorWithHexString:@"#F7F8FA"];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic = self.data[indexPath.section][indexPath.row];
    self.selDic = dic;
    NSString * selName = dic[@"sel"];
    SEL action = NSSelectorFromString(selName);
    if ([self respondsToSelector:action]) {
        [self performSelector:action withObject:nil afterDelay:0];
    }

}
#pragma mark -- 选择客户
-(void)clickCustomerName{
    LXChooseCustomViewController *vc = [[LXChooseCustomViewController alloc] init];
    [vc setToSendValue:^(KeHuInfoModel * _Nonnull model) {
        
//        NSString *str = [model.customerPhone substringToIndex:3];
//        str = [str stringByAppendingString:@"********"];
        [self.loanDic setValue: [NSString stringWithFormat:@"%@",model.customerName]  forKey:@"customerName"];
        self.keHuModel = model;
        [self.tableView reloadData];
    }];
    [self pushToNextController:vc];
}
#pragma mark -- 选择进厂日期
-(void)clickIntoTime{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"intoTime"] withIsMaxDate:YES withNoForward:NO WithTips:@"请选择进厂日期"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
        
        if([NSString compareDate:yearString withDate:self.loanDic[@"outTime"] withFormat:@"yyyy-MM-dd"]==-1){
            showAlert(@"进厂日期不能大于出厂日期");
        }
        else {
            [weakSelf.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
            [weakSelf.tableView reloadData];
            
        }
    };
}
#pragma mark -- 选择出厂日期
-(void)clickOutTime{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"outTime"] withIsMaxDate:YES withNoForward:NO WithTips:@"请选择出厂日期"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
        
        if([NSString compareDate:self.loanDic[@"intoTime"] withDate:yearString withFormat:@"yyyy-MM-dd"]==-1){
            showAlert(@"出厂日期不能小于进厂日期");
        }
        else {
            [weakSelf.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
            [weakSelf.tableView reloadData];
            
        }

    };
}

#pragma mark -- 选择应付日期日期
-(void)clickExceptPayTimeAction{
    [self.view endEditing:YES];
    DatePickView *datePicker =[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withDateStr:self.loanDic[@"exceptPayDate"] withIsMaxDate:YES withNoForward:NO WithTips:@"请选择出厂日期"] ;
    [datePicker showBoomView];
    [[[UIApplication sharedApplication].delegate window] addSubview:datePicker];
    WEAKSELF
    datePicker.block = ^(NSString *yearString){
        
        [weakSelf.loanDic setObject:yearString forKey:weakSelf.selDic[@"key"]];
        [weakSelf.tableView reloadData];
        
    };
}
#pragma mark -- 选择负责人
-(void)clickManager{
    [self.view endEditing:YES];
    
    LXChooseManagerViewController *vc = [[LXChooseManagerViewController alloc] init];
    WEAKSELF
    [vc setToSendValue:^(FuZeRenModel * _Nonnull model) {
        [weakSelf.loanDic setValue:model.realName forKey:@"chargeName"];
        weakSelf.chargeId = model.ID;
        weakSelf.fuZeModel = model;
        [weakSelf.tableView reloadData];
    }];
    vc.model = self.fuZeModel;
    [self pushToNextController:vc];
}



#pragma mark -- 选择车牌号
- (void)clickCarNum{
    
    LXChooseCarViewViewController *vc = [[LXChooseCarViewViewController alloc] init];
    vc.type = @"12";
    vc.comfrom = 1;
    [vc setToSendValue:^(CheLiangModel * _Nonnull model) {

        
        self.carModel = model;
        [self.loanDic setValue:model.carNum forKey:self.selDic[@"key"]];
        
        
        if(!StringIsEmpty(model.driverId)){
            self.keHuModel.driverId = model.ID;
            self.keHuModel.customerName = model.customerName;
//            NSString *str = [model.customerPhone substringToIndex:3];
//            str = [str stringByAppendingString:@"********"];
            [self.loanDic setValue: [NSString stringWithFormat:@"%@",model.customerName]  forKey:@"customerName"];
        }
       
        
        
        
        [self.tableView reloadData];
        
    }];
    [self pushToNextController:vc];
}
// MARK: 选择维修厂
-(void)clickRepairDepotAction{
    LXCooperationListViewController *vc = [[LXCooperationListViewController alloc] init];
    vc.type = 1;
    [vc setClickVcCallBlock:^(LXCooperationModel * _Nonnull model) {
        self.cooModel = model;
        [self.loanDic setValue:model.name forKey:@"repairCompanyName"];
        [self.tableView reloadData];
    }];
    [self pushToNextController:vc];
}


- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc]init];
    }
    return _data;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, kScreenHeight - kViewTopHeight - kComHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = White_Color;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}


#pragma mark -- 维修状态数据源
- (NSArray *)repairStatusData{
    
    _repairStatusData = @[
        
        @{
            @"title":@"维修中",
            @"sel":@(NO),
            @"id":@(1)
        }.mutableCopy,
        @{
            @"title":@"已修好",
            @"sel":@(NO),
            @"id":@(2)
        }.mutableCopy,
        
    ];
    
    for (NSMutableDictionary *dic in _repairStatusData) {
        if ([dic[@"id"] integerValue] == self.repairStatus) {
            [dic setValue:@(YES) forKey:@"sel"];
            break;
        }
    }
    
    return _repairStatusData;
}
#pragma mark -- 结算状态数据源
- (NSArray *)settlementStatusData{
    _settlementStatusData = @[
        
        @{
            @"title":@"未结算",
            @"sel":@(NO),
            @"id":@(1)
        }.mutableCopy,
        @{
            @"title":@"已结算",
            @"sel":@(NO),
            @"id":@(2)
        }.mutableCopy,
        @{
            @"title":@"已确认",
            @"sel":@(NO),
            @"id":@(3)
        }.mutableCopy,
    ];
    
    for (NSMutableDictionary *dic in _settlementStatusData) {
        if ([dic[@"id"] integerValue] == self.settlementStatus) {
            [dic setValue:@(YES) forKey:@"sel"];
            break;
        }
    }
    
    
    return _settlementStatusData;
}
#pragma mark -- 提车状态数据源
- (NSArray *)pickCarStatusData{
    _pickCarStatusData = @[
        
        @{
            @"title":@"未提车",
            @"sel":@(NO),
            @"id":@(1)
        }.mutableCopy,
        @{
            @"title":@"已提车",
            @"sel":@(NO),
            @"id":@(2)
        }.mutableCopy,
        
    ];
    
    for (NSMutableDictionary *dic in _pickCarStatusData) {
        if ([dic[@"id"] integerValue] == self.pickCarStatus) {
            [dic setValue:@(YES) forKey:@"sel"];
            break;
        }
    }
    
    
    return _pickCarStatusData;
    
}

#pragma mark ---- 确认新增/修改点击事件
- (void)clickBottomView:(UIButton *)sender{
    
   
    if (StringIsEmpty(self.loanDic[@"carNum"])) {
        showAlert(@"请选择车牌号");
        return;
    }
    
//    if (StringIsEmpty(self.loanDic[@"chargeName"])) {
//        showAlert(@"请选择负责人");
//        return;
//    }
    
    if (StringIsEmpty(self.loanDic[@"intoTime"])) {
        showAlert(@"请选择进厂日期");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"repairAmount"])) {
        showAlert(@"请输入应付维修金额");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"exceptPayDate"])) {
        showAlert(@"请选择应付日期");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"repairCompanyName"])) {
        showAlert(@"请选择修理厂");
        return;
    }
    
//    if (StringIsEmpty(self.loanDic[@"outTime"])) {
//        showAlert(@"请选择出厂日期");
//        return;
//    }
    
//    if (self.annexDataAry.count==0) {
//        showAlert(@"请上传维修图片");
//        return;
//    }
   
    
    NSMutableDictionary *dic = @{
        
        @"carId":self.carModel.ID, // 所属车辆id
        @"carNum":self.carModel.carNum,// 所属车辆车牌号
        @"chargeName":self.loanDic[@"chargeName"],// 负责人
        @"chargeId":self.chargeId,// 负责人id
        @"driverId":self.keHuModel.ID,// 租车客户id
        @"driverName":self.keHuModel.customerName,// 租车客户名称
        @"repairCompanyId":self.cooModel.ID,  //  维修公司id
        @"repairCompanyName":self.cooModel.name, //  维修公司名称
        @"intoTime":self.loanDic[@"intoTime"],//  进厂日期
        @"outTime":[NSString stringRemoveWithNull:self.loanDic[@"outTime"]], //  出厂日期
        @"distance":self.loanDic[@"distance"],// 行驶公里数
        @"exceptPayDate":self.loanDic[@"exceptPayDate"],// 应付日期
        @"repairAmount":self.loanDic[@"repairAmount"], //  维修金额
        @"repairContent":self.loanDic[@"repairContent"], //  维修内容,
        @"remark":self.loanDic[@"remark"],// 备注
        @"repairPics":self.annexDataAry, //  维修图片
        @"pickCarStatus":@(self.pickCarStatus),// 提车状态；1.未提车，2：已提车
        @"repairStatus":@(self.repairStatus),// 维修状态；1：维修中，2：已完成
//        @"settlementStatus":@(self.settlementStatus),// 结算状态；1：未结算，2：已结算，3：已确认
        
    }.mutableCopy;
    
    NSString *url;
    // 新增维修
    if (self.type==1) {
        url = [NSString stringWithFormat:@"%@",TrepairAdd_URL];
//        [dic setValue:@(self.remindDay) forKey:@"remindDay"];// 到期提醒
    }
    // 修改维修的时候需要传id
    else if (self.type==2) {
        url = [NSString stringWithFormat:@"%@",TrepairUpdate_URL];
        [dic setValue:self.repairModel.repairId forKey:@"repairId"];
    }

    
    [Tool showAlertTitle:@"是否确认提交" msg:nil cancelTitle:@"取消" commitBtnTitle:@"确定" cancelHandlerBlock:^{
    } commitHandlerBlock:^{
        [MSNetworkManager postRevertUrl:url params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
            
            if (isSuccess) {
                if (self.type==1) {
                    showAlert(@"新增维修成功");
                }
                else {
                    showAlert(@"修改维修成功");
                }
               
                
                // carModel 有值说明从详情页进入,返回时须刷新列表
                if (!ObjectIsEmpty(self.carModel)) {
                    kUserDefaultSetBool(YES, kRefreshStr);
                }
                [self.navigationController popViewControllerAnimated:YES];
            }
            
        } failure:^(NSUInteger code, NSString * _Nonnull message) {
        } withShowHud:YES];
    } onVC:self];
    
   
   
    
    
    
}

#pragma mark -- 到期提醒数据源
- (NSArray *)remindDayData{
    _remindDayData = @[

        @{
            @"title":@"提前3天",
            @"sel":@(NO),
            @"id":@(3)
        }.mutableCopy,
        @{
            @"title":@"提前5天",
            @"sel":@(NO),
            @"id":@(5)
        }.mutableCopy,
      
    ];

    for (NSMutableDictionary *dic in _remindDayData) {
        if ([dic[@"id"] integerValue] == self.remindDay) {
            [dic setValue:@(YES) forKey:@"sel"];
            break;
        }
    }


    return _remindDayData;
}
-(void)uploadImage:(NSArray *)ary{
    
    [self.annexAry removeLastObject];
    
    MBProgressHUD *hud = [MBProgressHUD showGif];
        
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        
    dispatch_async(queue, ^{
    
    [ary enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
        [MSNetworkManager postFilesUrl:POST_UploadFile_URL params:@{@"type":@"1"} formData:obj success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
            if (isSuccess) {
                
                NSString *imageUrl = [NSString stringWithFormat:@"%@%@",HTTP_URL,resonseObj[@"url"]];

                [self.annexAry addObject:@{
                    @"url":imageUrl
                }];
                [self.annexDataAry addObject:resonseObj[@"url"]];
                
                if (idx == ary.count - 1) {
//                    *stop = YES;
                    [hud hide];
                    [self.annexAry addObject:@{
                        @"url":@"upLoad_B",
                    }];
                    [self.tableView reloadData];
                }
                dispatch_semaphore_signal(semaphore);
            }
        } failure:^(NSUInteger code, NSString * _Nonnull message) {
            dispatch_semaphore_signal(semaphore);
            [hud hide];
        } withShowHud:NO];
        
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        
        
     }];
    });

}

@end
